using System;
using UnityEngine;
using Random = UnityEngine.Random;

namespace Gj
{
    public class RandomMultipleAnimationHelper : MultipleAnimationHelper
    {
        public bool randomStart;
        public bool randomEnd;
        public float radiusStartMin;
        public float radiusStartMax;
        public float radiusEndMin;
        public float radiusEndMax;
        public float angle = 360;
        public float offsetAngle;
        private Vector3 _end;
        private Vector3 _start;

        protected override void Init()
        {
            base.Init();
            _start = positionStart;
            _end = positionEnd;
        }

        private Vector2 GetPoint(Vector2 point, float radius)
        {
            return GetPoint(point, radius, Random.Range(0, angle) + offsetAngle);
        }

        private Vector2 GetPoint(Vector2 point, float radius, float a)
        {
            var radian = MathTools.Radians(a);
            var _x = point.x + radius * (float)Math.Cos(radian);
            var _y = point.y + radius * (float)Math.Sin(radian);
            return new Vector2(_x, _y);
        }

        private Vector3 GetPoint(Vector3 point, float radius)
        {
            return MathTools.ToUIVector3(GetPoint(MathTools.ToUIVector2(point), radius));
        }

        protected override void OnStart()
        {
            base.OnStart();
            if (randomStart) positionStart = GetPoint(_start, Random.Range(radiusStartMin, radiusStartMax));

            if (randomEnd) positionEnd = GetPoint(_end, Random.Range(radiusEndMin, radiusEndMax));
        }
    }
}